package com.luoran.bartalks.action;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.entity.biz.BtPosition;
import com.luoran.bartalks.entity.biz.BtRegion;
import com.luoran.bartalks.service.IBtCodingRuleService;
import com.luoran.bartalks.service.IBtPositionService;
import com.luoran.bartalks.service.IBtRegionService;
import com.luoran.bartalks.utils.EnumBillType;

@Controller
@RequestMapping("/reg")
public class BtRegAction {

	@Autowired
	private IBtPositionService iBtPositionService;

	@Autowired
	private IBtRegionService iBtRegionService;

	@Autowired
	private IBtCodingRuleService codingRuleService;

	private static final Logger log = LoggerFactory.getLogger(BtPositionAction.class);

	/**
	 * 获取区域
	 * 
	 * @return
	 */
	@GetMapping("/getRegion")
	@ResponseBody
	public JSONObject getRegion() {
		log.info("获取区域数据");
		JSONObject res = new JSONObject();
		List<BtRegion> regInfos = iBtRegionService.getRegionByCompanyId(UserContext.get().getCompanyId());
		res.put("obj", regInfos);
		res.put("state", true);
		return res;
	}

	// 生成编码
	@RequestMapping("/generateCode")
	@ResponseBody
	public JSONObject gennerateCompanyCoding() {
		return codingRuleService.generateCoding(EnumBillType.AREA, UserContext.get().getCompanyId());
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/addRegion")
	@ResponseBody
	public JSONObject addRegion(BtRegion reg) {
		log.info("新增区域数据>>" + reg.toString());
		JSONObject res = new JSONObject();
		if (StringUtils.isEmpty(reg.getRegionName())) {
			res.put("msg", "区域名称不可以为空");
			res.put("state", false);
			return res;
		} else if (reg.getRegionName().length() > 100) {
			res.put("msg", "区域名称长度限制为100");
			res.put("state", false);
			return res;
		}
		if (StringUtils.isEmpty(reg.getRegionCode())) {
			res.put("msg", "区域编码不可以为空");
			res.put("state", false);
			return res;
		} else if (reg.getRegionCode().length() > 100) {
			res.put("msg", "区域编码长度限制为100");
			res.put("state", false);
			return res;
		}else if (reg.getRegionDesc().length() > 500) {
			res.put("msg", "区域描述长度限制为500");
			res.put("state", false);
			return res;
		}
		// 编码是否重复
		BtRegion region = iBtRegionService.getRegionByRegCode(reg.getRegionCode(), UserContext.get().getCompanyId());
		if (region == null) {
			reg.setCompanyId(UserContext.get().getCompanyId());
			reg.setCreateTime(new Date());
			reg.setFounder(UserContext.get().getCompanyId());
			reg.setState(1);// 默认状态
			BtRegion btRegion = iBtRegionService.addRegion(reg);
			res.put("obj", btRegion);
			res.put("state", true);
			return res;
		} else {
			res.put("msg", "区域编码“" + reg.getRegionCode() + "”已被其他区域引用！");
			res.put("state", false);
			return res;
		}
	}

	/**
	 * 编辑
	 * 
	 * @param reg
	 * @return
	 */
	@PostMapping("/editRegion")
	@ResponseBody
	public JSONObject editRegion(BtRegion reg) {
		log.info("更新区域数据>>" + reg.toString());
		JSONObject res = new JSONObject();
		reg.setCompanyId(UserContext.get().getCompanyId());
		if (StringUtils.isEmpty(reg.getRegionName())) {
			res.put("msg", "区域名称不可以为空");
			res.put("state", false);
			return res;
		} else if (reg.getRegionName().length() > 100) {
			res.put("msg", "区域名称长度限制为100");
			res.put("state", false);
			return res;
		} else if (StringUtils.isEmpty(reg.getRegionDesc())) {
			// 设置公共属性
			reg.setCompanyId(UserContext.get().getCompanyId());
			reg.setCreateTime(new Date());
			reg.setFounder(UserContext.get().getCompanyId());
		} else if (reg.getRegionDesc().length() > 500) {
			res.put("msg", "区域描述长度限制为500");
			res.put("state", false);
			return res;
		}
		iBtRegionService.updateRegById(reg);
		res.put("msg", "更新成功！");
		res.put("state", true);
		return res;
	}

	/**
	 * 删除
	 * 
	 * @param reg
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public JSONObject deleteRegion(BtRegion reg) {
		log.info("更新区域数据>>reg:" + reg.getRegionName());
		JSONObject res = new JSONObject();
		try {
			// 根据id查询
			List<BtPosition> posits = iBtPositionService.getPositByParentRegPositionCode(reg.getRegionCode(),
					UserContext.get().getCompanyId());
			if (posits == null || posits.isEmpty()) {
				reg.setState(0);
				iBtRegionService.deleteRegById(reg);
				res.put("msg", "删除成功！");
				res.put("state", true);
			}
		} catch (Exception e) {
			log.error("更新区域数据>>Error:" + e.getMessage());
			res.put("msg", "删除失败！");
			res.put("state", false);
			return res;
		}
		return res;
	}

	/**
	 * 检测是否存在引用
	 * 
	 * @param reg
	 * @return
	 */
	@PostMapping("/check")
	@ResponseBody
	public JSONObject checkRegion(BtRegion reg) {
		log.info("检测是否存在引用>>reg:" + reg.getId());
		JSONObject res = new JSONObject();
		// 根据编码查询子节点的信息
		BtRegion btRegion = iBtRegionService.getRegionByRegCode(reg.getRegionCode(), UserContext.get().getCompanyId());
		if (StringUtils.isEmpty((btRegion.getParentRegCode()))) {
			List<BtPosition> posits = iBtPositionService.getpositsBylongCode(reg.getRegionCode(),
					UserContext.get().getCompanyId());
			if (!posits.isEmpty()) {
				res.put("msg", "该区域下存在位置信息！");
				res.put("state", true);
			}
		} else {
			List<BtPosition> posit = iBtPositionService.getPositByParentRegPositionCode(reg.getRegionCode(),
					UserContext.get().getCompanyId());
			if (posit != null && posit.size() != 0) {
				res.put("msg", "该区域下存在位置信息！");
				res.put("state", true);
			}
		}
		return res;
	}
}
